//
//  WatchViewController.m
//  Courses
//
//  Created by YiXuePxh on 15/4/7.
//  Copyright (c) 2015年 boyibang. All rights reserved.
//

#import "WatchViewController.h"
#import "WatchRecord.h"
#import "WatchViewCell.h"
#import "LeTVViewController.h"
@interface WatchViewController ()

@end

@implementation WatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"观看记录";
    //提示网络状态
    [self showNetStatus];
    //
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[WatchRecord  getWatchCourseLists] mutableCopy];
        NSLog(@"我的收藏数据%d",[self.items count]);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });

    // Do any additional setup after loading the view.
    
    //界面刷新
    UIRefreshControl* rc = [[UIRefreshControl alloc] init];
    [rc addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
    rc.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    self.refreshControl = rc;
    self.refreshControl.tintColor = [UIColor colorWithRed:16/255.0 green:101/255.0 blue:228/255.0 alpha:1.0];
}

- (void)refreshAction{
    NSLog(@"开始刷新！");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[WatchRecord  getWatchCourseLists] mutableCopy];
        NSLog(@"我的收藏数据%d",[self.items count]);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];
            [self.tableView reloadData];
        });
    });
}
//界面刷新结束


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 30;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    CGRect frame = AutoRectMake(0, 0, DEFAULT_WIDTH, 30, WTFAutoLayoutTypeBar);
//    UIView *viewHeader = [[UIView alloc] initWithFrame:frame];
//    viewHeader.backgroundColor = [WTFColors defaultBarColor];
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:frame];
//    label.textAlignment = NSTextAlignmentCenter;
//    label.textColor = [UIColor whiteColor];
//    [viewHeader addSubview:label];
//    
//    if (self.items.count > section)
//    {
//        WatchData* data = [self.items[section] watchDatas][0];
//        label.text = data.markDate;
//    }
//    
//    return viewHeader;
//}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.items[section] watchDatas].count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[WatchViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.items.count > indexPath.section)
    {
        if ([self.items[indexPath.section] watchDatas].count > indexPath.row)
        {
            cell.record = [self.items[indexPath.section] watchDatas][indexPath.row];
        }
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeTVViewController* leTV = [[LeTVViewController alloc] init];
    leTV.user_unique = @"a821022973";
    leTV.video_unique = @"ae05447730";
    [self.navigationController presentViewController:leTV animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
